package edu.ped.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import edu.ped.android.nfc.LabyrinthActivity;
import edu.ped.android.utils.TagsMatrice;

public class LabyrinthView extends View  {

	private static int MATRICE_HEIGHT=1;
	private static int MATRICE_WIDTH=1;
	private static int CIRCLE_RADIUS = 100;

	TagsMatrice matrice;
	Paint paint;

	public LabyrinthView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.STROKE);
		setBackgroundColor(Color.WHITE);
	}

	public void setMatrice(TagsMatrice matrice){
		this.matrice = matrice;
		MATRICE_HEIGHT = matrice.getHorizontal_size();
		MATRICE_WIDTH = matrice.getVertical_size();
		Log.i("PED", ""+MATRICE_HEIGHT+MATRICE_WIDTH);
		CIRCLE_RADIUS = 100 / Math.min(MATRICE_HEIGHT,MATRICE_WIDTH);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int tailleX=  (getWidth() /  MATRICE_WIDTH);
		int tailleY=  (getHeight() /   MATRICE_HEIGHT);

		//dessin de la grille
		//verticales
		for(int i = 0;i<MATRICE_WIDTH;i++){
			canvas.drawLine(getWidth()/MATRICE_WIDTH*i+1, 0, getWidth()/MATRICE_WIDTH*i+1, getHeight(), paint);
		}
		//horizontales
		for(int i = 0;i<MATRICE_HEIGHT;i++){
			canvas.drawLine(0, getHeight()/MATRICE_HEIGHT*i+1, getWidth(), getHeight()/MATRICE_HEIGHT*i+1, paint);
		}

		if(matrice!=null){
			for(int i=0;i<MATRICE_WIDTH;i++)
				for(int j=0;j<MATRICE_HEIGHT;j++){
					int sign = ((Integer)matrice.getTag(j, i).getValeur()).intValue();
					float centerX =j*tailleX+tailleX/2;
					float centerY = i*tailleY+tailleY/2;
					if(sign == LabyrinthActivity.CORRECT || sign == LabyrinthActivity.END){
						paint.setARGB(255, 10, 255, 10);
						paint.setShadowLayer(1, 1, 1, Color.GREEN);
						Log.i("PED","position"+j+i);
						canvas.drawCircle(centerX, centerY, CIRCLE_RADIUS, paint);	
						paint.setColor(Color.BLACK);
						paint.setShadowLayer(0, 1, 1, Color.GREEN);
					}
					else if(sign == LabyrinthActivity.MINE){
						canvas.drawColor(Color.RED);
						canvas.drawLine(centerX, centerY, centerX-CIRCLE_RADIUS, centerY-CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX-CIRCLE_RADIUS, centerY+CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX+CIRCLE_RADIUS, centerY-CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX+CIRCLE_RADIUS, centerY+CIRCLE_RADIUS, paint);
					}
				}
		}
	}
}
